/*
 *******************************************************************************
 *
 * Copyright (C) 2019-2020 Dialog Semiconductor.
 * This computer program includes Confidential, Proprietary Information
 * of Dialog Semiconductor. All Rights Reserved.
 *
 *******************************************************************************
 */

package com.example.suota.suota.activities;

import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.suota.suota.R;
import com.example.suota.suota.suota.AutoSuota;
import com.example.suota.suota.suota.HeaderInfo;
import com.example.suota.suota.suota.SuotaFile;
import com.example.suota.suota.suota.SuotaManager;


public class RunAutoSuotaActivity extends BaseSuotaActivity {

    private static final String TAG = "RunAutoSuotaActivity";

    private SuotaFile suotaFile;
    private AutoSuota autoSuota;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getBluetoothDevice() == null)
            return;

        suotaFile = initSuotaFile();
//        autoSuota = new AutoSuota(getLifecycle(), this, getSuotaFile(), getBluetoothDevice());
        autoSuota = new AutoSuota(this, getSuotaFile(), getBluetoothDevice());
        autoSuota.run(this);
    }

    protected int getLayout() {
        return R.layout.suotalib_activity_suota;
    }

    protected @NonNull CurrentlySendingMode getSendingModeDisplay() {
        return CurrentlySendingMode.ONLY_BLOCK;
    }

    protected SuotaManager getSuotaManager() {
        return autoSuota != null ? autoSuota.getSuotaManager() : null;
    }

    protected SuotaFile getSuotaFile() {
        if (suotaFile == null)
            suotaFile = initSuotaFile();
        return suotaFile;
    }

    protected BluetoothDevice getBluetoothDevice() {
        if (!getIntent().hasExtra(BluetoothDevice.class.getSimpleName())) {
            Log.e(TAG, "Bluetooth device not found as intent extra");
            finish();
            return null;
        }
        return (BluetoothDevice) getIntent().getExtras().get(BluetoothDevice.class.getSimpleName());
    }

    @Nullable
    private SuotaFile initSuotaFile() {
        if (!getIntent().hasExtra(SuotaFile.class.getSimpleName()) && getIntent().getData() == null) {
            Log.e(TAG, "Suota file not found as intent extra");
            finish();
            return null;
        }

        SuotaFile suotaFile;
        if (getIntent().hasExtra(SuotaFile.class.getSimpleName())) {
            suotaFile = new SuotaFile(getIntent().getExtras().getString(SuotaFile.class.getSimpleName()));
            suotaFile.setHeaderInfo(HeaderInfo.create(suotaFile.getFile()));
        } else {
            suotaFile = new SuotaFile(this, getIntent().getData());
        }
        return suotaFile;
    }
}
